<template>
  <div id="content">
    <globle-linear-border>
      <el-card class="searchBox">
        <el-form :inline="true">
          <el-form-item label="课程名称">
            <el-input v-model="pageQuery.otherParams.courseName" placeholder="输入课程名称搜索" clearable />
          </el-form-item>
          <el-form-item label="教师姓名">
            <el-input v-model="pageQuery.otherParams.tchName" placeholder="输入教师姓名搜索" clearable />
          </el-form-item>
          <el-form-item label="课程学年">
            <el-select v-model="pageQuery.otherParams.schoolYear" placeholder="选择学年搜索" clearable>
              <el-option v-for="item in schoolYearList" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="课程学期">
            <el-select v-model="pageQuery.otherParams.stage" placeholder="选择学期搜索" clearable>
              <el-option label="第一学期" value="1" />
              <el-option label="第二学期" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="年级">
            <el-input v-model="pageQuery.otherParams.grade" placeholder="输入年级搜索" clearable />
          </el-form-item>
          <el-form-item label="开课类型">
            <el-input v-model="pageQuery.otherParams.courseType" placeholder="输入开课类型搜索" clearable />
          </el-form-item>
          <el-form-item label="学时类型">
            <el-input v-model="pageQuery.otherParams.classHourType" placeholder="输入学时类型搜索" clearable />
          </el-form-item>
          <el-form-item label="成绩录入">
            <el-select v-model="pageQuery.otherParams.scoreIsBegin" placeholder="选择成绩录入状态搜索" clearable>
              <el-option label="录入中" value="1" />
              <el-option label="已停录" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <section>
              <el-button type="success" @click.stop="onMoreCilck">高级筛选</el-button>
              <el-button type="primary" icon="Search" @click.stop="getPageInfo">搜 索</el-button>
              <el-button type="warning" icon="RefreshRight" @click.stop="reset">重 置</el-button>
            </section>
          </el-form-item>
        </el-form>

      </el-card>
    </globle-linear-border>

    <globle-linear-border style="margin-top: 20px">
      <el-card class="mainBox">
        <span v-if="tableData.rows">
          <el-table :border="true" stripe ref="tableRef" :data="tableData.rows"
            cell-class-name="table-cell" header-cell-class-name="table-header-cell">
            <el-table-column property="grade" label="年级" width="80" />
            <el-table-column property="schoolYear" label="学年" width="130" />
            <el-table-column property="stage" label="学期" width="100" />
            <el-table-column property="courseCode" label="课程代码" width="100"/>
            <el-table-column property="courseName" label="课程名称" />
            <el-table-column property="tchName" label="教师名称" width="100"/>
            <el-table-column property="teachClass" label="教学班" />
            <el-table-column property="classHourType" label="学时类型" width="100"/>
            <el-table-column property="courseType" label="开课类型" />
            <el-table-column property="stuClass" label="已选人数" width="90">
              <template #default="scope">{{ scope.row.selectedNum }}/{{ scope.row.maxPerson }}</template>
            </el-table-column>
            <el-table-column label="成绩录入">
              <template #default="scope">
                <el-tag effect="dark" v-if="scope.row.scoreIsBegin == 1" dark size="small" type="success">录入中</el-tag>
                <el-tag effect="dark" v-else size="small" dark type="danger">已停录</el-tag>
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="330">
              <template #default="scope">
                <el-button type="primary" size="small" @click="onCheck(scope.row)">课程详情</el-button>
                <el-button type="success" size="small" @click="onAddScore(scope.row)">录入成绩</el-button>
                <el-button type="warning" size="small" @click="onScoreAnalyse(scope.row)">成绩分析</el-button>
                <el-button type="success" size="small" @click="onExpose(scope.row)">成绩导出</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination v-model:currentPage="pageQuery.pageParams.pageNum"
            v-model:page-size="pageQuery.pageParams.pageSize" :page-sizes="pageSizeOption" :background="true"
            :small="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.totalSize"
            @size-change="onSizeChange" @current-change="onCurrentChange" />
        </span>
      </el-card>
    </globle-linear-border>
    <!-- 弹窗 -->
    <suspense>
      <course-dialog ref="courseDialogRef" @onReGetData="getPageInfo"></course-dialog>
    </suspense>
    <more-params ref="moreParamsRef" @reload="reload"></more-params>
    <stu-drawer ref="stuDrawerRef"></stu-drawer>
    <analyse-drawer ref="analyseDrawerRef"></analyse-drawer>
    <export-dialog ref="exportDialogRef"></export-dialog>
  </div>
</template>

<script lang="ts" setup>
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessage, ElTable } from 'element-plus';
import { ICoursePageParams, IPageRes, ICourse, APIData, IScoreAnalyseByTchParams, IScoreAnalyseByTch } from '../../../type';
import { useUserStore } from '../../../store/user';
import { useAPI } from '../../../request/api';
import MoreParams from './components/MoreParams.vue';
import StuDrawer from './components/StuDrawer.vue';
import AnalyseDrawer from './components/scoreAnalyseDrawer/Index.vue'
import ExportDialog from './components/ExportDialog.vue';
//注意!!定义动态引入组件时需要使用defineAsyncComponent，并且在template中需要搭配suspense组件使用,它是一个异步组件解决方案。
const CourseDialog = defineAsyncComponent(() => import('./components/CourseDialog.vue'));

const store = useUserStore();

onMounted(() => {
  findAllCourseSchoolYear();
  getPageInfo();
});

const api = useAPI();
const moreParamsRef = ref();
const courseDialogRef = ref();
const stuDrawerRef = ref();
const analyseDrawerRef = ref();
const exportDialogRef = ref();
const chartData = ref<IScoreAnalyseByTch>();
const tableRef = ref<InstanceType<typeof ElTable>>();
const pageSizeOption = [5, 10, 15, 20];
const schoolYearList = ref([] as { label:string; value:string }[]);
const pageQuery = reactive<ICoursePageParams>({
  otherParams: {
    courseName: '',
    schoolYear: '',
    stage: '',
    grade: '',
    courseType: '',
    classHourType: '',
    // courseIsBegin: '',
    courseIsDelete: 0,
    scoreIsBegin: '',
    workId: store.userInfo.workId

    //以下在更多弹窗里面
    // courseCode: '',
    // openCollege: '',
    // place: '',
    // credit: '',
    // week: '',
    // weekDay: '',
    // orderNum: '',
    // teachClass: '',
    // totalHours: '',
    // allowBand: '',
    // workId: ''
    // studentId: '',
  },
  pageParams: {
    pageNum: 1,
    pageSize: pageSizeOption[1],
    sortField: '',
    sortType: '',
  },
});
const tableData = ref({} as IPageRes);

const reset = () => {
  pageQuery.otherParams = {
    courseName: '',
    tchName: '',
    schoolYear: '',
    stage: '',
    grade: '',
    courseType: '',
    classHourType: '',
    courseIsDelete: 0,
    courseIsBegin: ''
  }
  pageQuery.pageParams = {
  pageNum: 1,
  pageSize: pageSizeOption[1],
  sortField: '',
  sortType: ''
}
  getPageInfo();
}

const onAddScore = (row: ICourse) => {
  if(row.scoreIsBegin != 1) {
    ElMessage.error('该课程暂未开始录入成绩');
    return;
  }
  stuDrawerRef.value.open(row)
}

const onScoreAnalyse = async (row: ICourse) => {
  const params: IScoreAnalyseByTchParams = { courseId: row.id };
  const res: APIData = await api.score.findScoreAnalyseByTch(params);
  if (res.code === 200 && res.data.personalRankList) {
    chartData.value = res.data;
    analyseDrawerRef.value.open(row,chartData.value);
  } else if (res.code === 200 && !res.data.personalRankList) ElMessage.success('该课程暂无学生分数数据');
  else ElMessage.error(res.message);
}

const onCheck = (row: ICourse) => {
  courseDialogRef.value.openDialogByCheck(row.id);
};

const onMoreCilck = () => {
  moreParamsRef.value.open();
}

const reload = (data: any) => {
  tableData.value = data;
}

//导出
const onExpose = (row: ICourse) => {
  exportDialogRef.value.open(row);
};

//分页大小
const onSizeChange = (val: number) => {
  pageQuery.pageParams.pageSize = val;
  pageQuery.pageParams.pageNum = 1;
  getPageInfo();
};
//分页页码
const onCurrentChange = (val: number) => {
  pageQuery.pageParams.pageNum = val;
  getPageInfo();
};

const findAllCourseSchoolYear = async () => {
  const res: APIData = await api.course.findAllCourseSchoolYear();
  if (res.code == 200) {
    const originList = res.data;
    schoolYearList.value = originList.map((item: string) => {
      return { label: `${item}学年`, value: `${item}` }
    })
  }
};

const getPageInfo = async () => {
  const res = await api.course.findPage(pageQuery);
  tableData.value = res.data;
};
</script>

<style lang="scss" scoped>
#content {
  height: 100%;
  padding: 14px 32px;

  .searchBox {
    &-item {
      margin-right: 10px;
    }

    :deep(.el-card__body) {
      display: flex;
      justify-content: space-between;
    }

    section {
      display: flex;
    }

    :deep(.el-input__wrapper) {
      width: 200px;
    }
  }

  .mainBox {
    :deep(.el-checkbox__inner) {
      background-color: var(--main-color);
      transition: background 0.3s ease;
    }

    :deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
      background-color: var(--active-color);
      transition: background 0.3s ease;
    }
  }
}
</style>
